package com.example.lxj.sources.controller;

import java.util.List;
import java.util.Optional;

import com.example.lxj.sources.entity.TbMaintenanceRecords;
import com.example.lxj.sources.service.ITbMaintenanceRecordsService;
import com.example.lxj.base.annoation.Permission;
import com.example.lxj.base.controller.BaseController;
import com.example.lxj.base.entity.AjaxRes;
import com.example.lxj.base.entity.PageDTO;
import com.example.lxj.base.entity.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * 设备维护Controller
 *
 * @author lxj
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/sources/maintenance_records")
public class TbMaintenanceRecordsController extends BaseController {
    @Autowired
    private ITbMaintenanceRecordsService tbMaintenanceRecordsService;

    /**
     * 查询设备维护列表
     */
    @Permission(value = "sources:maintenance_records:list")
    @GetMapping("/list")
    public TableDataInfo list(TbMaintenanceRecords tbMaintenanceRecords, PageDTO pageDTO) {
        startPage(Optional.ofNullable(pageDTO.getPageNo()).orElse(1), Optional.ofNullable(pageDTO.getPageSize()).orElse(10));
        List<TbMaintenanceRecords> list = tbMaintenanceRecordsService.selectTbMaintenanceRecordsList(tbMaintenanceRecords);
        return getDataTable(list);
    }

    /**
     * 获取设备维护详细信息
     */
    @Permission(value = "sources:maintenance_records:query")
    @GetMapping(value = "/{id}")
    public AjaxRes getInfo(@PathVariable("id") Long id) {
        return success(tbMaintenanceRecordsService.selectTbMaintenanceRecordsById(id));
    }

    /**
     * 新增设备维护
     */
    @Permission(value = "sources:maintenance_records:add")
    @PostMapping
    public AjaxRes add(@RequestBody TbMaintenanceRecords tbMaintenanceRecords) {
        return toAjax(tbMaintenanceRecordsService.insertTbMaintenanceRecords(tbMaintenanceRecords));
    }

    /**
     * 修改设备维护
     */
    @Permission(value = "sources:maintenance_records:edit")
    @PutMapping
    public AjaxRes edit(@RequestBody TbMaintenanceRecords tbMaintenanceRecords) {
        return toAjax(tbMaintenanceRecordsService.updateTbMaintenanceRecords(tbMaintenanceRecords));
    }

    /**
     * 删除设备维护
     */
    @Permission(value = "sources:maintenance_records:remove")
    @DeleteMapping("/{ids}")
    public AjaxRes remove(@PathVariable("ids") Long[] ids) {
        System.out.println(ids);
        return toAjax(tbMaintenanceRecordsService.deleteTbMaintenanceRecordsByIds(ids));
    }
}
